﻿// Write a program to check if in a given expression the brackets are put correctly.
// Example of correct expression: ((a+b)/5-d).
// Example of incorrect expression: )(a+b)).
using System;

public class CheckBrackets
{
    public static void Main()
    {
        Console.Write("Enter expression to check the brackets: ");
        string expression = Console.ReadLine();
        if (IsBracketsBalanced('(', ')', expression))
        {
            Console.WriteLine("The brackets are put correctly.");
        }
        else
        {
            Console.WriteLine("The brackets are NOT put correctly.");
        }
    }

    public static bool IsBracketsBalanced(char leftParenthesis, char rightParenthesis, string expression)
    {
        int balanced = 0;
        if (expression != string.Empty && expression != null)
        {
            foreach (char c in expression)
            {
                if (c.Equals(leftParenthesis))
                {
                    balanced++;
                }
                else if (c.Equals(rightParenthesis))
                {
                    if (--balanced < 0)
                    {
                        return false;
                    }
                }
            }
        }

        return balanced == 0;
    }
}